home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / snip9503 / frand.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-14  |  1.1 KB  |  38 lines

  1. /*
  2. **  FRAND.C - Public domain by Larry Hudson
  3. */
  4.  
  5. #include <math.h>
  6. #include <time.h>
  7.  
  8. #define TEN_PI 31.41592653589793
  9. #define E      2.718281828459045
  10.  
  11. /*--------------------------------------------------------------+
  12. |           Return random double between 0.0 and 1.0            |
  13. |                                                               |
  14. |    If n is negative it will randomize the seed, based on the  |
  15. |        current MSDOS time.                                    |
  16. |    If n is zero it will return the next random number.        |
  17. |    If n is positive it will set the seed to a value based on  |
  18. |        the value of n.                                        |
  19. +--------------------------------------------------------------*/
  20.  
  21. double frandom(int n)
  22. {
  23.       static double seed = E;
  24.       double dummy;
  25.       time_t tim;
  26.  
  27.       if (n < 0)
  28.       {
  29.             time(&tim);
  30.             seed = (double)tim;
  31.       }
  32.       else if (n > 0)
  33.             seed = (double)n * E;
  34.  
  35.       seed = modf(seed * TEN_PI + E, &dummy);
  36.       return seed;
  37. }
  38.